// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var css_tallyDetail = require("./bulkstockTallyDetail.css");
	var css_bulkstockAbnormalDetail = require("./bulkstockAbnormalDetail.css");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var recieveNoticeId;
	var tallyingId;
	var inventoryId;
	var type;
	var taskDetail;
	var bulkstockInventoryDetail;

	var init = function(page) {
		inventoryId = page.params.inventoryId || base.getUrlParam("inventoryId");
		inquireBulkstockInventoryDetail(inventoryId);
		initEvent();
		router.init();

		$("#leftMenu1").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu1 #newTaskList").parent(".leftMenu-item").addClass("leftMenu-active");
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//异常理货
		$(".abnormalTally").on("click", function() {
			$("#submitContainerTallying").removeClass("none");
			$(".abnormalSignDeliveryContent").addClass("none");
			$(".abnormalTally").find("b").css("color", "#FF7344");
			$(".abnormalSign").find("b").css("color", "#000");
			$(".line-gray img").css("left", "0px");
		});

		//异常签收
		$(".abnormalSign").on("click", function() {
			$("#submitContainerTallying").addClass("none");
			$(".abnormalSignDeliveryContent").removeClass("none");
			$(".abnormalSign").find("b").css("color", "#FF7344");
			$(".abnormalTally").find("b").css("color", "#000");
			$(".line-gray img").css("left", "111px");
			databind.fillForm($(".abnormalSignDeliveryContent"), bulkstockInventoryDetail.datas[0]);

			$.each($(".signAbnormalFile"), function(i, val) {
				showImg($(this).parent(), val.value);
			});
			$.each($(".signSoundRecorde"), function(i, val) {
				showImg($(this).parent(), val.value);
			});
			$.each($(".signPreFile"), function(i, val) {
				showImg($(this).parent(), val.value);
			});
			$.each($(".signSuffixFile"), function(i, val) {
				showImg($(this).parent(), val.value);
			});
			databind.disableInput($(".abnormalSignDeliveryContent"));
		});




	};


	//查询融资箱货明细
	var inquireBulkstockInventoryDetail = function(inventoryId) {
		base.fnAjax({
			action: "inquireBulkstockInventoryDetail.json",
			oData: {
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					bulkstockInventoryDetail = data;
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#tallyDetailTpl").html();
					var html = juicer(tpl, data);
					$(".tallyDetail_main_content").html(html);
					$.each($(".bulkstockFilesUuids"), function(i, val) {
						showImg($(this).parent().find(".uploadImgP"), val.value);
					});
					$.each($(".suffixTallyFiles"), function(i, val) {
						showImg($(this).parent(), val.value);
					});
					$.each($(".preTallyFiles"), function(i, val) {
						showImg($(this).parent(), val.value);
					});

					//录音文件
					$.each($("#soundRecorde"), function(i, val) {
						var dom = $(this).parent();
						var fileUuids = val.value.split(",");
						var html = "";
						var imgSrc = "";
						for (var i = 0; i < fileUuids.length; i++) {
							imgSrc = fileUuids[i];
							if (imgSrc.length != 0) {
								//				html = "<a href='/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc +"'></a><img  src=\"/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  />";
								html = '<object width="100" height="50" data="/finance/api/v1.0/downloadFile.file?fileUuid=' + imgSrc + '"></object>';
								$(dom).append(html);
							}

						}
					});

					//异常图片
					$.each($("#abnormalFile"), function(i, val) {
						showImg($(this).parent(), val.value);
					});

				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	}

	//显示图片
	var showImg = function(dom, fileUuid) {
		var fileUuids = fileUuid.split(",");
		var html = "";
		var imgSrc = "";
		for (var i = 0; i < fileUuids.length; i++) {
			imgSrc = fileUuids[i];
			if (imgSrc.length != 0) {
				//				html = "<a href='/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc +"'></a><img  src=\"/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  />";
				html = "<a target='_blank' href='/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc + "'><img  src='/finance/api/v1.0/downloadFile.file?fileUuid=" + imgSrc + "'class='finance-field-img'/></a>";
				$(dom).html(html);
			}

		}
	}

	return {
		init: init
	};
});